package main

import (
	"fmt"
	"html/template"
	"log"
	"logfilter/filter"
	"net/http"
	"os/exec"
)

func getIndex(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("../resource/index.html")
	t.Execute(w, nil)
}

func logFilter(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	if r.Method == "POST" {
		beginDate :=r.Form["date_begin"][0]
		endDate :=r.Form["date_end"][0]
		_, bug :=r.Form["bug"]
		_, optimize :=r.Form["optimize"]
		logstring :=r.Form["logstring"][0]

		fmt.Fprintf(w,"<html><body>")
		result := filter.Do(beginDate, endDate, bug, optimize, logstring);
		fmt.Fprintf(w,"%s", result)

		bottom := `<a href=”#” onClick=”javascript :history.back(-1);”>返回上一页</a>`
		fmt.Fprintf(w,"%s", bottom)
		fmt.Fprintf(w,"</html></body>")
	}
}

func main() {
	exec.Command("cmd", "/c", "start", "http://127.0.0.1:9527").Start()
	http.HandleFunc("/", getIndex)
	http.HandleFunc("/logfilter", logFilter)
	err := http.ListenAndServe(":9527", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
